package com.weaver.shuiguict.action;

import com.alibaba.fastjson.JSONObject;
import com.weaver.shuiguict.entity.DocDetailTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.docs.docs.DocManager;
import weaver.docs.docs.ShareManageDocOperation;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class ChangeDocStatusnwAction implements Action {

    private static final Logger LOGGER = LoggerFactory.getLogger("ChangeDocStatusnwAction");


    public String defaultParam;

    @Override
    public String execute(RequestInfo requestInfo) {
        LOGGER.info("start execute = ChangeDocStatusnwAction");
        String fbfw = "";
        String mhlmkey = "";
        String wdid = "";
//        String sffbddcd = "";

        //获取表单主字段的信息
        try {

            Property[] propertyArry = requestInfo.getMainTableInfo().getProperty();
            LOGGER.info("start execute = requestInfo.getMainTableInfo().getProperty();");
            DetailTableInfo detailTableInfo = requestInfo.getDetailTableInfo();

            LOGGER.info("start execute = requestInfo.detailTableInfo()");
            for (Property property : propertyArry) {
                String name = property.getName();
                String value = Util.null2String(property.getValue());
                //获取内外网的值判断是内网还是外网  fbfw  0是外网发布 1是内网发布1,0是内外网发布
                if ("fbfw".equals(name)) {

                    fbfw = value;
                }
                //获取门户栏目的id  mhlm
                if ("mhlm".equals(name)) {

                    mhlmkey = value;
                }
                if ("wd".equals(name)) {

                    wdid = value;
                }
//                // 如果发布到多菜单
//                if ("sffbddcd".equals(name)) {
//
//                    sffbddcd = value;
//                }
                LOGGER.info("name = {} value = {}",name,value);

            }
            List<DocDetailTable> docDetailTables = new ArrayList<>();

//            if ("0".equals(sffbddcd)){
                docDetailTables = this.getdDocDetailList(detailTableInfo);
//            }

//            DocDetailTable docDetailTable = new DocDetailTable();
//            docDetailTable.setMhlm(mhlmkey);
//            docDetailTables.add(docDetailTable);

            for (DocDetailTable detailTable : docDetailTables) {

                String mhlm = detailTable.getMhlm();

                // 执行文档复制
                boolean handle = handle(fbfw, mhlm, wdid);
                LOGGER.info(" mhlm = {} handle = {}",mhlm,handle);
            }
        } catch (Exception e) {
            e.printStackTrace();
            new BaseBean().writeLog("ChangeDocStatusnwAction异常："+e.getMessage());
            return Action.FAILURE_AND_CONTINUE;
        }
        return Action.SUCCESS;
    }

    // 获取明细表的数据
    public List<DocDetailTable> getdDocDetailList(DetailTableInfo detailTableInfo){
        DetailTable[] detailTable = detailTableInfo.getDetailTable();
        List<DocDetailTable> docDetailTables = new ArrayList<>();
        for (DetailTable table : detailTable) {
            Row[] row = table.getRow();
            for (Row row1 : row) {
                Cell[] cell = row1.getCell();
                DocDetailTable docDetailTable = new DocDetailTable();
                boolean flag = false;
                for (Cell cell1 : cell) {
                    String name = cell1.getName();
                    String value = cell1.getValue();
                    if (Objects.equals(name, "mhcd")){
                        flag = true;
                        docDetailTable.setMhcd(value);

                    }
                    if (Objects.equals(name, "mhlm")){
                        flag = true;
                        docDetailTable.setMhlm(value);
                    }
                }
                if (flag){
                    docDetailTables.add(docDetailTable);
                }
            }
            LOGGER.info("table {}", JSONObject.toJSONString(table));
        }
        return docDetailTables;
    }
    public boolean handle(String fbfw, String mhlmkey, String wdid) throws Exception {

        LOGGER.info("execute copy fbfw = {}, mhlm = {} wdid = {}", fbfw, mhlmkey, wdid);
        // 根据 门户类名 查询到 对应的 内外网门户 id
        /**
         * wwml=外网目录
         * nwmlid=内网目录
         * nwwmlid=内外网目录ID
         * 外网发布=0
         * 内网发布=1
         * 微信公众号=2
         */
        String queryConfigSql = "";
        if ("0".equals(fbfw) || "0,2".equals(fbfw) || "2,0".equals(fbfw)) {
            //查询的列不同
            queryConfigSql = "select wwml from uf_nwwcddy where mlid = ?";
        } else if ("1".equals(fbfw) || "1,2".equals(fbfw) || "2,1".equals(fbfw)) {
            queryConfigSql = "select nwmlid from uf_nwwcddy where mlid = ?";
        } else if ("0,1".equals(fbfw) || "1,0".equals(fbfw) || "0,1,2".equals(fbfw) || "0,2,1".equals(fbfw) || "1,0,2".equals(fbfw) || "1,2,0".equals(fbfw) || "2,0,1".equals(fbfw) || "2,1,0".equals(fbfw)) {
            queryConfigSql = "select nwwmlid from uf_nwwcddy where mlid = ?";
        } else {
            queryConfigSql = "select nwwmlid from uf_nwwcddy where mlid = ?";
        }

        RecordSet queryConfigSqlSet = new RecordSet();
        LOGGER.info("execute queryConfigSql  = {}", queryConfigSql);
        boolean queryConfigSqlFlag = queryConfigSqlSet.executeQuery(queryConfigSql, mhlmkey);

        String mlid = "";
        //通过配置表去查目录id
        if (queryConfigSqlFlag && queryConfigSqlSet.next()) {
            if ("0".equals(fbfw) || "0,2".equals(fbfw) || "2,0".equals(fbfw)) {
                //查询的列不同
                mlid = queryConfigSqlSet.getString("wwml");
            } else if ("1".equals(fbfw) || "1,2".equals(fbfw) || "2,1".equals(fbfw)) {
                mlid = queryConfigSqlSet.getString("nwmlid");
            } else if ("0,1".equals(fbfw) || "1,0".equals(fbfw) || "0,1,2".equals(fbfw) || "0,2,1".equals(fbfw) || "1,0,2".equals(fbfw) || "1,2,0".equals(fbfw) || "2,0,1".equals(fbfw) || "2,1,0".equals(fbfw)) {
                mlid = queryConfigSqlSet.getString("nwwmlid");
            } else {
                mlid = queryConfigSqlSet.getString("nwwmlid");
            }
        }

//            String updateSql = "Update docdetail Set seccategory = ? Where id = ? ";
//            RecordSet recordSet = new RecordSet();

        return executeMove(fbfw, mlid, wdid);
    }
    public boolean executeMove(String fbfw, String mlid, String wdid) throws Exception {
        User user = new User(1);
        for (String s : wdid.split(",")) {
            DocManager docManager = new DocManager();
            //文档id
            docManager.setId(Integer.parseInt(s));
            //用户id
            docManager.setUserid(user.getUID());
            //⽤户类型
            docManager.setUsertype(user.getLogintype());
            //ip地址
            docManager.setClientAddress("127.0.0.1");
            //⽬标⽬录id
            docManager.setSeccategory(Integer.parseInt(mlid));

            // check 修改成 将 文档 复制
            int newDocId = docManager.copyDoc();
            LOGGER.info("oldDocId = {}",s);
            LOGGER.info("newDocId = {}",newDocId);
            LOGGER.info("move to = {}",mlid);
//            //移动
//            docManager.moveDoc();

            //默认共享刷新
            ShareManageDocOperation manager = new ShareManageDocOperation();
            manager.copyMoveDocShareBySec(Integer.parseInt(mlid), Integer.parseInt(s));

            // 将复制的也更新一下
            manager.copyMoveDocShareBySec(Integer.parseInt(mlid), newDocId);
            new BaseBean().writeLog("ChangeDocStatusnwAction====>" + mlid + "property.getValue()===>" + s + "=======>");
        }
        return true;
    }
}
